package vistasCasaCentral;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import tp_ad.RMIClient.RMIClientCSR;
import tp_ad.bean.OficinaDeVentasBean;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class JFrameGanancia extends javax.swing.JInternalFrame {

	{
		try {
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JButton btnAceptar;
	private JButton btnCancelar;
	private JTextField txtGanancia;
	private JLabel lblGanancia;
	private JLabel jLabelODV;
	private JComboBox jComboBoxODV;
	private static JFrameGanancia instancia;

	/**
	* Auto-generated main method to display this 
	* JInternalFrame inside a new JFrame.
	*/
	
	public static JFrameGanancia getInstancia(){
		if (instancia==null){
			instancia = new JFrameGanancia();
		}
		return instancia;
	}
	
	public JFrameGanancia() {
		super();
		initGUI();
	}
	
    public void cargarODV(){
        List<OficinaDeVentasBean> oficinas= RMIClientCSR.getInstancia().cargarComboODV();
        for (OficinaDeVentasBean odvBean:oficinas){
            this.jComboBoxODV.addItem(odvBean.getCiudad());
        }
    }

	private void initGUI() {
		try {
			this.setPreferredSize(new java.awt.Dimension(400, 184));
			this.setBounds(0, 0, 400, 184);
			setVisible(true);
			this.setTitle("Alta de Cliente");
			getContentPane().setLayout(null);
			this.setSize(400, 184);
			{
				btnAceptar = new JButton();
				getContentPane().add(btnAceptar, "Center");
				btnAceptar.setText("Aceptar");
				btnAceptar.setBounds(59, 103, 98, 26);
				btnAceptar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {						
							OficinaDeVentasBean oficinaBean = new OficinaDeVentasBean();							
							Float ganancia = new Float("0");
							try{ganancia = Float.parseFloat(txtGanancia.getText());}catch(Exception e){JOptionPane.showMessageDialog(JFrameGanancia.getInstancia(),"Porcentaje debe ser un numero");return;}
							if(ganancia>100 || ganancia<0){
								JOptionPane.showMessageDialog(JFrameGanancia.getInstancia(),"Porcentaje debe ser un numero entre 0 y 100");
								return;
							}							
							oficinaBean.setGanancia(ganancia);							
							cargarODVSearch(oficinaBean);
							boolean result = RMIClientCSR.getInstancia().updateODV(oficinaBean);
							if (result){
								JOptionPane.showMessageDialog(JFrameGanancia.getInstancia(),"El % Ganancia actualizado");
								dispose();
							}
							else
								JOptionPane.showMessageDialog(JFrameGanancia.getInstancia(),"No se pudo actualizar el % Ganancia");				

					}
				}
						);
			}
			{
				btnCancelar = new JButton();
				getContentPane().add(btnCancelar);
				btnCancelar.setText("Cancelar");
				btnCancelar.setBounds(211, 103, 107, 26);
				btnCancelar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						dispose();
					}
				});
			}
			{
				lblGanancia = new JLabel();
				getContentPane().add(lblGanancia);
				lblGanancia.setText("% Ganancia: ");
				lblGanancia.setBounds(59, 19, 81, 14);
			}
			{
				txtGanancia = new JTextField();
				getContentPane().add(txtGanancia);
				txtGanancia.setBounds(179, 16, 166, 21);
			}
			{
				jLabelODV = new JLabel();
				getContentPane().add(jLabelODV);
				jLabelODV.setText("Oficina de Ventas:");
				jLabelODV.setBounds(34, 56, 145, 16);
			}
			{
				ComboBoxModel jComboBox1Model2 = 
						new DefaultComboBoxModel(
								new String[] {});
				jComboBoxODV = new JComboBox();
				getContentPane().add(jComboBoxODV);
				jComboBoxODV.setModel(jComboBox1Model2);
				jComboBoxODV.setBounds(179, 54, 166, 21);
			}
			cargarODV();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void cargarODVSearch(OficinaDeVentasBean oficinaBean){
		String odv = ((String)jComboBoxODV.getSelectedItem());
		oficinaBean.setCiudad((odv.substring(0)));
	}
	
}